/***********************************************************************************************************************
*                                                                                                                      *
* SPLASH build system v0.1                                                                                             *
*                                                                                                                      *
* Copyright (c) 2013 Andrew D. Zonenberg                                                                               *
* All rights reserved.                                                                                                 *
*                                                                                                                      *
* Redistribution and use in source and binary forms, with or without modification, are permitted provided that the     *
* following conditions are met:                                                                                        *
*                                                                                                                      *
*    * Redistributions of source code must retain the above copyright notice, this list of conditions, and the         *
*      following disclaimer.                                                                                           *
*                                                                                                                      *
*    * Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the       *
*      following disclaimer in the documentation and/or other materials provided with the distribution.                *
*                                                                                                                      *
*    * Neither the name of the author nor the names of any contributors may be used to endorse or promote products     *
*      derived from this software without specific prior written permission.                                           *
*                                                                                                                      *
* THIS SOFTWARE IS PROVIDED BY THE AUTHORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED   *
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL *
* THE AUTHORS BE HELD LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES        *
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR       *
* BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT *
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE       *
* POSSIBILITY OF SUCH DAMAGE.                                                                                          *
*                                                                                                                      *
***********************************************************************************************************************/

#ifndef BuildGraph_h
#define BuildGraph_h

/**
	@brief A graph of targets and intermediate files.
	
	The core data structure for the entire build system.
 */
class BuildGraph
{
public:
	BuildGraph(std::string metadir, std::string srcdir, std::string outdir);
	virtual ~BuildGraph();
	
	void AddNode(BuildGraphNode* node);
	void AddNodeWithPath(BuildGraphNode* node, std::string path, std::string arch);
	void AddAlternatePath(BuildGraphNode* node, std::string path, std::string arch);
	
	std::string GetMetaDir()
	{ return m_metadir; }
	
	std::string GetOutDir()
	{ return m_outdir; }
	
	BuildGraphNode* GetNodeForPath(std::string path, std::string arch);
	
	size_t size()
	{ return m_nodes.size(); }
	
	BuildGraphNode* GetNode(size_t i)
	{ return m_nodes[i]; }
	
	void Clear();
	
	void AddTarget(BuildGraphNode* node, std::string name);
	
	typedef std::map<std::string, BuildGraphNode*> BuildMapType;
	typedef std::map< std::string, std::unordered_set<std::string> > ProfileMapType;
	
	BuildMapType::iterator targetbegin()
	{ return m_targets.begin(); }
	
	BuildMapType::iterator targetend()
	{ return m_targets.end(); }
	
	bool IsTargetForArch(std::string name, std::string arch)
	{
		return IsTarget(name + "-" + arch);
	}
	
	bool IsTarget(std::string name)
	{
		return m_targets.find(name) != m_targets.end();
	}
	
	BuildGraphNode* GetTargetForArch(std::string name, std::string arch)
	{
		return GetTarget(name + "-" + arch);
	}
	
	BuildGraphNode* GetTarget(std::string name)
	{
		if(m_targets.find(name) != m_targets.end())
			return m_targets[name];
		else
			FatalError("Target \"%s\" does not exist\n", name.c_str());
	}
	
	
	BuildMapType::iterator FindTarget(std::string name)
	{ return m_targets.find(name); }
	
	BuildMapType::iterator FindTargetForArch(std::string name, std::string arch)
	{ return m_targets.find(name + "-" + arch); }
	
	ProfileMapType::const_iterator ProfileDirBegin()
	{ return m_profiledirs.begin(); }
	
	ProfileMapType::const_iterator ProfileDirEnd()
	{ return m_profiledirs.end(); }
	
	void AddProfileDir(std::string arch, std::string path)
	{ m_profiledirs[arch].emplace(path);  }
	
	std::string GetSourceDir()
	{ return m_srcdir; }
	
	void AddTest(BuildGraphNode* node);
	
	size_t testsize()
	{ return m_tests.size(); }
	
	BuildGraphNode* GetTest(size_t i)
	{ return m_tests[i]; }
	
	BuildGraphNode* GetTest(std::string name);
	
	/**
		@brief Adds a module record
		
		If the module already exists, the old one is kept and the new one is deleted.
	 */
	void AddModuleInfo(std::string s, AntikernelModuleInfo* modinfo)
	{
		if(m_rpcinfo.find(s) == m_rpcinfo.end())
			m_rpcinfo[s] = modinfo;
		else
			delete modinfo;
	}
	
	void WriteModuleInfo(std::string buildroot);
	
protected:
	//The authoritative list of nodes in the graph (ordering has no meaning)
	std::vector<BuildGraphNode*> m_nodes;
	
	//Map of source/header file paths to nodes
	std::map<std::pair<std::string, std::string>, BuildGraphNode*> m_pathnodes;
	
	//Array of test cases
	std::vector<BuildGraphNode*> m_tests;
	
	//Metadata directory
	std::string m_metadir;
	
	//Source directory
	std::string m_srcdir;
	
	//Binary directory
	std::string m_outdir;
	
	//List of nodes declared as targets (sink nodes)
	BuildMapType m_targets;
	
	//RPC protocol metadata
	std::map<std::string, AntikernelModuleInfo*> m_rpcinfo;
	
	///List of C/C++ object directories for each architecture, used for profiling
	ProfileMapType m_profiledirs;
};

#endif
